/*
 * @Author: DaHuaZhuXi
 * @Date: 2020-12-01 09:39:47
 * @LastEditTime: 2021-01-13 17:50:03
 * @LastEditors: DaHuaZhuXi
 * @Description: 战斗相关函数
 */
import People from '@/class/People.js';
import { createPeo, createGood, o2o } from "@/class/Tool.js";

//初始化我方
export function initPeos() {
  game.curSave.myTeam.forEach(peo => {
    let peoClone = JSON.parse(JSON.stringify(peo));
    peoClone.__proto__ = new People;
    peoClone.init("our", this.map, this.peos, this.elements, this.enemys);
    this.peos.push(peoClone);
    this.tempPeos.push(peoClone);
  });
  console.log("我方", this.peos);
  putPeos.call(this,"myTeam");
}

//初始化敌方
export function initEnemys() {
  game.battleTempEnemys = [];
  //根据当前地形，单位概率生产单位
  let o = data.places.find(p => p.id == this.placeId);
  let place = o2o(o);

  //let types = place.unit;
  let myTeamCount = game.curSave.myTeam.length;
  //let count = common.random(myTeamCount,myTeamCount+2);
  let types = [[19,1]];
  let count = 1;

  while (count > 0) {
    let unitAndProb = types[common.random(0, types.length - 1)];
    let prob = unitAndProb[1];
    if (Math.random() < prob) {
      let peo = createPeo(unitAndProb[0]);
      game.battleTempEnemys.push(JSON.parse(JSON.stringify(peo)));
      peo.__proto__ = new People;
      peo.init("enemy", this.map, this.peos, this.elements, this.enemys);
      createEnemyEquips(peo);
      peo.updateAbility();

      this.enemys.push(peo);
      this.tempEnemys.push(peo);
      count--;
    }
  }

  putPeos.call(this,"enemys");
  console.log("生成敌方：", this.enemys);
}

//根据敌方类型创建装备并穿戴
export function createEnemyEquips(peo) {
  let defaultEquip = data.peos.find(p => p.id == peo.type).$defaultEquip;
  defaultEquip = defaultEquip.replace(/'/g, "\"");
  defaultEquip = JSON.parse(defaultEquip);
  for (let key in defaultEquip) {
    let val = defaultEquip[key];
    let type = val[common.random(0, val.length - 1)];
    if (type > 0) {
      peo.addEquip(key, createGood(type));
    }
  };
}

//初始化地图元素（障碍物等）
export function initElements() {
  //生成数量最大值
  let maxCount = parseInt(((this.mapSize.xMax + 1) * (this.mapSize.yMax + 1) - this.peos.length - this.enemys.length) /
    5); 

  let count = common.random(parseInt(maxCount / 2), maxCount);

  // 已占用坐标，地图元素生成的禁用坐标包含非占位元素
  let banPoints = JSON.parse(JSON.stringify(this.map.banPoints));

  for (let i = 0; i < count; i++) {
    let point = common.creatPoint(this.mapSize.xMax, this.mapSize.yMax, banPoints);
    // 随机地形类别
    const typeId = common.random(0, data.terrains.length - 1);
    let o = data.terrains.find(e => e.id == typeId);
    let ele = o2o(o);
    ele.name = ele.type + i;
    ele.type = ele.id;
    ele.id = common.createUniqueId();
    ele.x = point[0];
    ele.y = point[1];
    ele.eva = 0;
    ele.hhb = 0;
    ele._equips = {};
    ele.hpMax = ele.hp;
    ele._type = "element";
    this.elements.push(ele);

    // 地图元素生成的禁用坐标包含非占位元素
    banPoints.push([ele.x, ele.y]);
    if(ele.seat == 1){  
      this.map.banPoints.push([ele.x, ele.y]);
    }
  }
}

//放置人员
function putPeos(type) {
  let y = type == "myTeam" ? this.mapSize.yMax : 0;
  let ary = type == "myTeam" ? this.peos : this.enemys;
  let start = parseInt(((this.mapSize.xMax + 1) - ary.length) / 2);
  start = start < 0 ? 0 : start;
  ary.forEach((p, i) => {
    p.x = start + i;
    p.y = y;
    this.map.banPoints.push([p.x, p.y]);
  })
}

//初始化地图
export function initMap() {
  let option = {};
  option.peos = this.peos;
  option.elements = this.elements;
  option.enemys = this.enemys;
  option.$map = this.$map;
  let docWidth = document.documentElement.clientWidth;
  this.unitSize = option.unitSize = parseInt(docWidth / this.mapDiv);
  option.cols = Math.round((this.mapSize.xMax + 1) * 1);
  option.rows = Math.round((this.mapSize.yMax + 1) * 1);
  this.map.init(option);
}